package com.pilipili.zeus.eduService.controller;


import com.alibaba.excel.util.StringUtils;
import com.pilipili.commonutils.Result;
import com.pilipili.zeus.eduService.client.VodClient;
import com.pilipili.zeus.eduService.entity.EduVideo;
import com.pilipili.zeus.eduService.service.EduVideoService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 番剧视频 前端控制器
 *
 * @author Unlimited_Blade_Works
 * @since 2021-05-26
 */
@RestController
@RequestMapping("/eduService/video")
public class EduVideoController {
    @Autowired
    private EduVideoService videoService;
    @Autowired
    private VodClient vodClient;

    @ApiOperation(value = "添加视频")
    @PostMapping("addVideo")
    public Result addVideo(@RequestBody EduVideo eduVideo){
        videoService.save(eduVideo);
        return Result.ok();
    }

    @ApiOperation(value = "番剧id获取视频信息")
    @GetMapping("getVideo/{id}")
    public Result getVideo(@PathVariable String id){
        EduVideo eduVideo = videoService.getById(id);
        return Result.ok().data("video",eduVideo);
    }

    @ApiOperation(value = "更新视频")
    @PostMapping("updateVideo")
    public Result updateVideo(@RequestBody EduVideo eduVideo){
        videoService.updateById(eduVideo);
        return Result.ok();
    }

    @ApiOperation(value = "调用Vod模块删除云端和数据库视频")
    @DeleteMapping("removeVideo/{id}")
    public Result removeVideo(@PathVariable String id){
        String videoSourceId = videoService.getById(id).getVideoSourceId();
        if(!StringUtils.isEmpty(videoSourceId)) {
            vodClient.deleteVideo(videoSourceId);
        }
        boolean flag = videoService.removeById(id);
        return flag?Result.ok():Result.error();
    }
    @ApiOperation(value = "读取courseSortExcel中的表格,写入数据库")
    @PostMapping("addVideos")
    public Result addVideos(MultipartFile file){
        videoService.saveVideosDataByExcel(file,videoService);
        return Result.ok();
    }
}

